//
//  ControlUIHandler.swift
//  MeenoHybrid
//
//  Created by alyadan on 2016/10/17.
//  Copyright © 2016年 alyadan. All rights reserved.
//

import ObjectMapper
import RxCocoa
import YD_ProgressHUD

class MNWebViewMainHandler: HybridBaseHandler {
    
    override init(){
        super.init()
        name = "MNWebViewMainHandler"
        
    }
    
    static var popupModelCallBack:BlockWarp?
    
    //temp method
    
    func updateDataModel(data: [String: Any], callback:BlockWarp) {
        guard let dataModel = data["dataModel"] as? MeenoHybrid.DictType else{
            callbackParamError(callback: callback.task)
            return
        }
        
        if let vcDataModel = HybridVCDataModel(JSON: dataModel){
            (self.hybridVCModel as? HybridVCModel)?.hybridVCDataModel.value = vcDataModel
        }else {
            callbackParamError(callback: callback.task)
        }
        
    }
    
    func popupModal(data: [String: Any], callback:BlockWarp) {
        guard let url = data["url"] as? String else{
            callbackParamError(callback: callback.task)
            return
        }
        
        let vcDataModel = HybridVCDataModel(url: url)
        vcDataModel.bgClear = 0
        
        let vcModel = HybridVCModel(parentViewModel: hybridVCModel, dataModel: vcDataModel)
        
        vcModel.hybridWebViewModel.parentWebViewModel = self.hybridWebViewModel
        
        let vc = vcModel.creatViewController()!
        
        if let nav = hybridVCModel?.MVVM_navigationController {
            vc.modalPresentationStyle = .overCurrentContext
            vc.modalTransitionStyle = .crossDissolve
            
            if let tabbar = nav.tabBarController {
                tabbar.definesPresentationContext = true
                tabbar.present(vc, animated: true, completion: nil)
                
            }else {
                nav.definesPresentationContext = true
                nav.present(vc, animated: true, completion: nil)
            }
            MNWebViewMainHandler.popupModelCallBack = callback
        }
        
        
    }
    
    func closeModal(data: [String: Any], callback:BlockWarp) {
        
        self.hybridVCModel?.closeSelf(animated: true)
        
        if let x = MNWebViewMainHandler.popupModelCallBack {
            self.callbackSuccess(callback: x.task, data: data)
        }
        
        MNWebViewMainHandler.popupModelCallBack = nil
    }
    
    func closeMoreViewController(data: [String: Any], callback:BlockWarp) {
        guard let ids = data["ids"] as? [String] else{
            callbackParamError(callback: callback.task)
           return
        }
       
        
        var vcs = [UIViewController]()
        
        hybridVCModel?.closeMoreViewController(ids: ids, vcs: &vcs)
        
        if vcs.count > 0 {
            if let nav = hybridVCModel?.MVVM_navigationController{
               var currentVCs = nav.viewControllers
                currentVCs = currentVCs.filter({ (x: UIViewController) -> Bool in
                    !vcs.contains(x)
                })
                nav.setViewControllers(currentVCs, animated: false)
                return
            }
        }
        
        callbackFail(callback: callback.task, error: 10, subError: 0, errMsg: "为找到对应页面")
        
    }
    //end
    
    
    public var dataTransferCallBack : WVJBResponseCallback?
    
    func openWebViewController(data: [String: Any], callback:BlockWarp) {
        if  let vcDataModel = HybridVCDataModel(JSON: data) {
            if vcDataModel.url == nil {
                print("url data wrong")
               return
            }
            
            dataTransferCallBack = callback.task
            
            let vcModel = HybridVCModel(parentViewModel: hybridVCModel, dataModel: vcDataModel)
            vcModel.hybridWebViewModel.parentWebViewModel = self.hybridWebViewModel
            hybridVCModel?.MVVM_navigationController?.pushVcModel(vcModel: vcModel)
            
        }else {
            
           print("error data wrong")
            
        }
    }
    
    func openWebViewControllerWithDataTransfer(data: [String: Any],
                                               callback:BlockWarp) {
       self.openWebViewController(data: data, callback: callback)
    }
    
    func transferData(data: [String: Any], callback:BlockWarp) {
        
        if let model =  self.hybridWebViewModel as? HybridWebViewModel {
            (model.parentWebViewModel as? HybridWebViewModel)?.mainHandler.onTransferData(data: data)
        }
    }
    
    func onTransferData(data: MeenoHybrid.DictType) {
       callbackSuccess(callback: dataTransferCallBack, data: data)
    }
    
    
    func closeSelfViewController(data: [String: Any], callback:BlockWarp) {
        
        guard let animated = data["anim"] as? Bool else{
            callbackParamError(callback: callback.task)
           return
        }
        
        
        hybridVCModel!.closeSelf(animated: animated)
        
        
    }
    
    
    //MARK: webView
    func reloadWebView(data: [String: Any], callback:BlockWarp) {
        
        if hybridWebViewModel?.isReloding.value == false {
            self.hybridWebViewModel?.isReloding.value = true
        }
    }
    
    func refresh(data: [String: Any], callback:BlockWarp) {
        guard let refreshType = data["refreshType"] as? Int else {
            callbackParamError(callback: callback.task)
            return
        }
        
        hybridWebViewModel?.refreshType.value = refreshType
    }
    
    
    func refreshFinish(data: [String: Any], callback:BlockWarp) {
        hybridWebViewModel?.isUPRefresh.value = false
        hybridWebViewModel?.isDownRefresh.value = false
    }
    
    func UpRefresh(data: [String: Any], callback:BlockWarp) {
        hybridWebViewModel?.isUPRefresh.value = true
    }
    
    func downRefresh(data: [String: Any], callback:BlockWarp) {
        hybridWebViewModel?.isUPRefresh.value = true
    }
    
    func enableDrag(data: [String: Any], callback:BlockWarp) {
        hybridWebViewModel?.dragable.value = true
    }
    
    func disableDrag(data: [String: Any], callback:BlockWarp) {
        hybridWebViewModel?.dragable.value = false
    }
    
    
    
    func showToast(data: [String: Any], callback:BlockWarp) {
        
        guard let str = data["content"] as? String else {
            callbackParamError(callback: callback.task)
            return
        }
        
        YD_HUDCenter.share.svpHud.showMessage(message: str)
    }
    
    func showProgressDialog(data: [String: Any], callback:BlockWarp){
        guard let msg = data["content"] as? String else {
            callbackParamError(callback: callback.task)
            return
        }
        
       YD_HUDCenter.share.svpHud.showMessage(message: msg)
            
    }
    
}

//MARK: DataModel
/**
 * @param data
 {
 titleType = 0;  // 0 代表普通， 1 表示输入框
 url = "view/userViews/productDetail.html"; //地址
 isHideNavBar = 0; //是否隐藏导航栏
 centerParam =     ( //中间 数组
 {
 type = 0; //0文字， 1 图片
 param = "\U4ea7\U54c1\U8be6\U7ec6"; //文本 或 图片.png
 }
 );
 
 leftParam =     ( //左边 数组
 {
 leftType = 0; // 0：使用原生返回方法，1：使用H5方法
 param = "btn_back_nor";
 type = 1;
 }
 );
 rightParam =     ( //右边 数组
 {
 type = 1;
 param = "btn_collect_nor";
 },
 {
 type = 1;
 param = "btn_share_nor";
 }
 );
 navBgColor
 pageId
 
 
 }
 
 */


public class HybridVCDataModel :NSObject, Mappable {
    //MARK: test
    
    public var url: String!
    public var titleType: Bool?
    public var isHideNavBar: Bool? = false
    public var centerParam: [MeenoHybrid.DictType]?
    public var leftParam: [MeenoHybrid.DictType]?
    public var rightParam: [MeenoHybrid.DictType]?
    public var navBgColor: String?
    public var pageId: String?
    
    public var isHidenComment: Bool? = true
    public var searchBarParam: MeenoHybrid.DictType?
    
    //custom
    public var bgClear = 1
    //end
    
    public required init?(map: Map) {
        
    }
    
    public init(url: String){
        self.url = url
    }
    
     public func mapping(map: Map) {
        
        url <- map["url"]
        titleType <- map["titleType"]
        isHideNavBar <- map["isHideNavBar"]
        centerParam <- map["centerParam"]
        leftParam <- map["leftParam"]
        rightParam <- map["rightParam"]
        navBgColor <- map["navBgColor"]
        isHidenComment <- map["isHidenComment"]
        pageId <- map["pageId"]
        searchBarParam <- map["searchBarParam"]
    }
}
